/**
 * 
 */
package service;

import util.JankExecutors;

import java.util.concurrent.ExecutorService;

/**
 * https://www.jianshu.com/p/7a1612b1431c
 * 两条线程循环打印1-100
 * 
 * @author jiangnk001
 *
 */
public class MultiThreadPrintNumber {

	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		PrintServer pServer = new PrintServer();
        ExecutorService threadPool = JankExecutors.newFixedSizeExecutorPool(2, "print");
        threadPool.submit(pServer);
        threadPool.submit(pServer);
	}
}

class PrintServer implements Runnable{
	
	private int num = 1;
	
	@Override
	public void run(){
		while(true){
			synchronized (this) {
				notify();
				if (num <= 100) {
					System.out.println(Thread.currentThread().getName() + " " + num);
					num++;
					try {
						wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				} else {
					break;
				}
			} 							
		}
	}
	
} 
